package com.yellowpage.bigdata.action;

import java.io.UnsupportedEncodingException;
import java.util.*;

import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.bigdata.data.DefaultOption;
import com.yellowpage.bigdata.service.NspFatallogRequsetSumService;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.RegionService;
import com.yellowpage.tool.Excelexport;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.bigdata.data.NspOnofflogTaRegionMonthEntity;
import com.yellowpage.bigdata.service.NspOnofflogTaRegionMonthEntityService;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;

public class NspOnofflogTaRegionMonthEntityAction extends BaseAction {

    private JSONArray result;

    public String RESULT_QUERY = "query";

    private Configuration configuration;

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    private List<NspOnofflogTaRegionMonthEntity> nspOnofflogTaRegionMonthEntityList = new ArrayList<NspOnofflogTaRegionMonthEntity>();
    private List<Map> resultList = new ArrayList<Map>();

    private NspOnofflogTaRegionMonthEntity nspOnofflogTaRegionMonthEntity;
    private NspOnofflogTaRegionMonthEntityService nspOnofflogTaRegionMonthEntityService;

    private String selectedId;
    private String[] selectedIds;

    private String query_logDate;
    private String query_logDate_end;
    private String query_regionCode;


    private RegionService regionService;
    private NspFatallogRequsetSumService nspFatallogRequsetSumService;


    public NspOnofflogTaRegionMonthEntityService getNspOnofflogTaRegionMonthEntityService() {
        return nspOnofflogTaRegionMonthEntityService;
    }

    public void prepare() {
        super.prepare();
        if (this.selectedId != null && !this.selectedId.equals("")) {
            this.nspOnofflogTaRegionMonthEntity = nspOnofflogTaRegionMonthEntityService.getNspOnofflogTaRegionMonthEntity(Long.parseLong(selectedId));
        }
    }

    /**
     * 根据当前用户id获取所属区域
     */
    public String getRegionSql() {
        Map session = ActionContext.getContext().getSession();
        User user = (User) session.get("userObject");
        String function_code = "OMC_CLIENT_LOG_COUNT_DAY_REGIONCOUNT";
        String regionsql = regionService.getRegion(user.getUserId(), function_code, "REGION_CODE");
        return regionsql;
    }

    /**
     * ajax获取各省名称,存入session，避免重复查询影响性能
     */
    public String queryChannel() {
        Map session = ActionContext.getContext().getSession();
        List regionNames = (List) session.get("regionNames");
        if (regionNames == null || regionNames.size() == 0) {
            resultList.add(nspFatallogRequsetSumService.getRegionMap(getRegionSql()));
            regionNames = resultList;
            session.put("regionNames", resultList);
        }
        this.result = JSONArray.fromObject(regionNames);
        return SUCCESS;
    }

    /**
     * 获取前台查询条件
     */
    private Map getQueryMap() {
        Map queryMap = new HashMap();
        String regionsStr = "(";//regionNames拼接成查询list
        if (query_regionCode != null && !"".equals(query_regionCode)) {
            try {
                query_regionCode = java.net.URLDecoder.decode(query_regionCode, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String[] regions = query_regionCode.replaceAll("，", ",").split(",");
            for (String region : regions) {
                regionsStr += "\"" + region.trim() + "\",";
            }
            regionsStr = regionsStr.substring(0, regionsStr.length() - 1);

        }
        regionsStr += ")";
        queryMap.put("logDate", query_logDate);
        queryMap.put("query_logDate_end", query_logDate_end);
        queryMap.put("regionCode", regionsStr);
        return queryMap;
    }

    /**
     * 根据条件分页查询
     */
    public String queryByPage() {
        initPageRender();
        resultList = nspOnofflogTaRegionMonthEntityService.queryNspOnofflogTaRegionMonthEntityByPage(getQueryMap(), pageRender);
        setPageRenderToRequest();

        return RESULT_QUERY;
    }

    /**
     * 引用excel导出工具
     */
    public void exportExcel() {
        List<Map> excelList = nspOnofflogTaRegionMonthEntityService.queryAllNspOnofflogTaRegionDayEntity(getQueryMap());
        LinkedHashMap<String, String> namemap = new LinkedHashMap<String, String>();

        //给出中文名称,注：此key要和数据库column一样，否则无法导出
        namemap.put("LOG_DATE", "月份");
        namemap.put("REGION_CODE", "地区名称");
        namemap.put("IMEI_MONTH_COUNT", "当月活跃用户数");
        namemap.put("ACTIVE_COUNT", "主动活跃数");
        namemap.put("PASSIVE_COUNT", "被动活跃数");
        namemap.put("IMEI_ALL_COUNT", "累计用户数");
        namemap.put("IMEI_NEW_COUNT", "新增用户数");
        namemap.put("CREATE_USER", "创建用户");
        namemap.put("CREATE_TIME", "创建时间");
        namemap.put("LAST_UPDATE_USER", "最后更新用户");
        namemap.put("LAST_UPDATE_TIME", "最后更新时间");
        namemap.put("NOTE", "备注");
        Excelexport.exportExcel(excelList, this.getClass().getName(), namemap);
    }

    /**
     * ajax获取图表数据
     */
    public String getChart() throws UnsupportedEncodingException {
        resultList = nspOnofflogTaRegionMonthEntityService.queryAllNspOnofflogTaRegionMonthSumByDate(getQueryMap());
        if (resultList != null && resultList.size() != 0) {
            //x轴列表
            List xList = new ArrayList();
            //y轴统计纬度对应值
            List yList = new ArrayList();
            Iterator<Map> it = resultList.iterator();
            while (it.hasNext()) {
                Map map = it.next();
                xList.add(map.get("REGION_CODE"));
                yList.add(map.get("IMEI_NEW_COUNT"));
            }

            DefaultOption o = new DefaultOption();
            //设置所有统计纬度名称，可以设置多个 o.setLegends("新增","月新增")
            o.setLegends("新增");

            //设置图表标题和子标题
            o.setText("日新增用户分省统计", "统计日期:" + query_logDate + "-" + query_logDate_end);

            //设置x轴坐标列表
            o.setCategoryAxisList(xList);

            //设置y轴某一纬度对应名称和值，可设置多个
            o.setLineData("新增数", yList);

            //jackson返回
            HttpServletResponse response = ServletActionContext.getResponse();
            o.getOption(response);
        }
        return null;
    }

    /**
     * getters and setters
     *
     * @return
     */
    public void setNspOnofflogTaRegionMonthEntity(NspOnofflogTaRegionMonthEntity nspOnofflogTaRegionMonthEntity) {
        this.nspOnofflogTaRegionMonthEntity = nspOnofflogTaRegionMonthEntity;
    }

    public NspOnofflogTaRegionMonthEntity getNspOnofflogTaRegionMonthEntity() {
        return nspOnofflogTaRegionMonthEntity;
    }


    public void setNspOnofflogTaRegionMonthEntityService(NspOnofflogTaRegionMonthEntityService nspOnofflogTaRegionMonthEntityService) {
        this.nspOnofflogTaRegionMonthEntityService = nspOnofflogTaRegionMonthEntityService;
    }

    public List<NspOnofflogTaRegionMonthEntity> getNspOnofflogTaRegionMonthEntityList() {
        return nspOnofflogTaRegionMonthEntityList;
    }

    public void setNspOnofflogTaRegionMonthEntityList(List<NspOnofflogTaRegionMonthEntity> nspOnofflogTaRegionMonthEntityList) {
        this.nspOnofflogTaRegionMonthEntityList = nspOnofflogTaRegionMonthEntityList;
    }

    public String getSelectedId() {
        return selectedId;
    }

    public void setSelectedId(String selectedId) {
        this.selectedId = selectedId;
    }

    public String[] getSelectedIds() {
        return selectedIds;
    }

    public void setSelectedIds(String[] selectedIds) {
        this.selectedIds = selectedIds;
    }

    public List<Map> getResultList() {
        return resultList;
    }

    public void setResultList(List<Map> resultList) {
        this.resultList = resultList;
    }

    public String getQuery_logDate() {
        return this.query_logDate;
    }

    public void setQuery_logDate(String logDate) {
        this.query_logDate = logDate;
    }

    public String getQuery_logDate_end() {
        return query_logDate_end;
    }

    public void setQuery_logDate_end(String query_logDate_end) {
        this.query_logDate_end = query_logDate_end;
    }

    public RegionService getRegionService() {
        return regionService;
    }

    public void setRegionService(RegionService regionService) {
        this.regionService = regionService;
    }

    public NspFatallogRequsetSumService getNspFatallogRequsetSumService() {
        return nspFatallogRequsetSumService;
    }

    public void setNspFatallogRequsetSumService(NspFatallogRequsetSumService nspFatallogRequsetSumService) {
        this.nspFatallogRequsetSumService = nspFatallogRequsetSumService;
    }


    public String getQuery_regionCode() {
        return query_regionCode;
    }

    public void setQuery_regionCode(String query_regionCode) {
        this.query_regionCode = query_regionCode;
    }

    public JSONArray getResult() {
        return result;
    }
}
